package com.leetcode.algorithm.y22.m07.w4;

/**
 * 997. 找到小镇的法官
 * 
 * https://leetcode.cn/problems/find-the-town-judge/
 * 
 * @author jie.deng
 *
 */
class Question0997Solution01 {

	public int findJudge(int n, int[][] trust) {
		// 遍历 trust，统计每个节点的「入度」和「出度」
		int[] in = new int[n];
		int[] out = new int[n];
		for (int[] arr : trust) {
			in[arr[1] - 1]++;
			out[arr[0] - 1]++;
		}

		for (int i = 0; i < in.length; i++) {
			if (in[i] == n - 1 && out[i] == 0) {
				return i + 1;
			}
		}
		return -1;
	}

}